package com.atguigu.ucenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.vo.UcenterMember;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.service.MemberService;
import com.atguigu.ucenter.vo.LoginVo;
import com.atguigu.ucenter.vo.RegisterVo;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/ucenter/member")
//@CrossOrigin
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        String token = memberService.login(loginVo);
        return R.ok().data("token", token);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Member member = memberService.getById(memberId);
        //对数据的nickName进行解密
//        Base64.Decoder decoder=Base64.getDecoder();
//        byte[] decode = decoder.decode(member.getNickname());
//        String decodeNickName = new String(decode);
//        member.setNickname(decodeNickName);
        return R.ok().data("userInfo", member);
    }

    //根据token字符串的获取用户信息
    @PostMapping("getInfoUc/{id}")
    public UcenterMember getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        Member member = memberService.getById(id);
        //对数据的nickName进行解密
//        Base64.Decoder decoder=Base64.getDecoder();
//        byte[] decode = decoder.decode(member.getNickname());
//        String decodeNickName = new String(decode);
//        member.setNickname(decodeNickName);
        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(member, ucenterMember);
        return ucenterMember;
    }

    @GetMapping(value = "countregister/{day}")
    public R registerCount(
            @PathVariable String day) {
        Integer count = memberService.countRegisterByDay(day);
        return R.ok().data("countRegister", count);
    }

    @GetMapping("wxLoginInfo/{openId}")
    public R getWxLoginInfo(@PathVariable String openId){
        Member member = memberService.getByOpenid(openId);
        //对NicekName进行解密
        byte[] decode = Base64.decode(member.getNickname());
        String decodeNickName = new String(decode);
        member.setNickname(decodeNickName);
        return R.ok().data("member",member);
    }
}

